package com.zrrd.yunchmall.order.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.yunchmall.config.RabbitConfig;
import com.zrrd.yunchmall.order.entity.OrderReturnApply;
import com.zrrd.yunchmall.order.service.IOrderReturnApplyService;
import com.zrrd.yunchmall.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 订单退货申请 前端控制器
 * </p>
 *pageNum: 1
 * pageSize: 10
 * id: 123
 * status: 0
 * createTime: 2024-01-03
 * handleMan: xiaomi
 * handleTime: 2024-01-10
 * @author LiYe
 * @since 2024-01-15
 */
@RestController
@RequestMapping("/returnApply")
@Api(tags = "订单服务-退货处理")
public class OrderReturnApplyController {
    @Autowired
    private IOrderReturnApplyService applyService;
    @Autowired
    private RabbitTemplate rabbitTemplate;//注入一个Rabbit模板实例用来发布消息

    @PostMapping("/update/status/{id}")
    @ApiOperation("退货处理")
    public ResponseResult updateStatus(@PathVariable Integer id, @RequestBody OrderReturnApply apply) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", id);
        updateWrapper.set("status", apply.getStatus());
        updateWrapper.set("handle_man", apply.getHandleMan());
        updateWrapper.set("handle_note", apply.getHandleNote());
        updateWrapper.set("handle_time", LocalDateTime.now());
        if (apply.getStatus() == 1) {//同意退货
            updateWrapper.set("company_address_id", apply.getCompanyAddressId());
            updateWrapper.set("return_amount", apply.getReturnAmount());
        }
        if (apply.getStatus() == 2) {//确认收到退货
            //生成一个mq消息，通知商品服务修改商品库存
            Map map = new HashMap();
            map.put("info", "退货修改库存");
            map.put("productId", apply.getProductId());//商品id
            map.put("productCount", apply.getProductCount());//退货数量
            rabbitTemplate.convertAndSend(RabbitConfig.EXCHANGE_NAME, "mall.return", map);

            updateWrapper.set("receive_man", "当前登录用户");
            updateWrapper.set("receive_time", LocalDateTime.now());//收到退货的时间
            updateWrapper.set("receive_note", apply.getReceiveNote());//收到退货的备注
        }
        applyService.update(updateWrapper);
        return new ResponseResult(200, "处理完成");
    }

    @GetMapping("/{id}")
    @ApiOperation("查询退货处理详情")
    public ResponseResult detail(@PathVariable Integer id) {
        OrderReturnApply apply = applyService.getById(id);
        return new ResponseResult(200, "查询成功", apply);
    }

    @GetMapping("/list")
    @ApiOperation("处理记录列表")
    public ResponseResult list(@RequestParam int pageNum,
                               @RequestParam int pageSize,
                               Integer id, Integer status, String handleMan, String createTime, String handleTime) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (id != null) {
            queryWrapper.eq("id", id);
        } else {
            if (status != null) {
                queryWrapper.eq("status", status);
            }
            if (!StringUtils.isEmpty(handleMan)) {
                queryWrapper.like("handle_man", handleMan);
            }
            if (!StringUtils.isEmpty(createTime)) {
                queryWrapper.between("create_time", createTime + " 00:00:00", createTime + " 23:59:59");
            }
            if (!StringUtils.isEmpty(handleTime)) {
                queryWrapper.between("handle_time", handleTime + " 00:00:00", handleTime + " 23:59:59");
            }
        }
        return new ResponseResult(200, "查询成功", applyService.page(new Page<>(pageNum, pageSize),
                queryWrapper));
    }
}
